//
//  IDocTextCell.m
//  TestCutCopyAndParse
//
//  Created by 侯 甲 on 13-8-21.
//  Copyright (c) 2013年 侯 甲. All rights reserved.
//

#import "IDocTextCell.h"
#import "INHtmlElement.h"
#import "CoreTextLabel.h"
#import "CLGloble.h"
#import "UIView+Utils.h"

@interface IDocTextCellView : UIView{
    
}
@property (nonatomic,assign)BOOL isLastParagraph;
@property(nonatomic,strong)CoreTextLabel *coreTextLabel;
@property (nonatomic, assign) CGRect rc;

@property (nonatomic,strong) UIImageView *finishChanLinLogo;

- (void)fillDataWithTextArr:(CLDocumentModel *)textArr andElement:(INHtmlElement *)element;
@end

@implementation IDocTextCellView
- (id)initWithFrame:(CGRect)frame{
    if((self = [super initWithFrame:frame])){
        self.backgroundColor = [UIColor clearColor];
        _coreTextLabel = [[CoreTextLabel alloc] init];
        _coreTextLabel.backgroundColor = [UIColor clearColor];
        [self addSubview:_coreTextLabel];
        
        UIImageView *finishLogoImg = [[UIImageView alloc]initWithFrame:_rc];
        [finishLogoImg setImage:[UIImage imageNamed:@"doc_finish_logoIcon.png"]];
        [self addSubview:finishLogoImg];

    }
    return self;
}

- (void)fillDataWithTextArr:(CLDocumentModel *)textArr andElement:(INHtmlElement *)element{
    _coreTextLabel.frame = CGRectMake(I_DOC_TEXT_MARGIN_SPACE, 20, I_DOC_CONTENT_WIDTH, element.textHeight+10);//为统一与标题简介间隔由10改为20
    _coreTextLabel.element = element;
    _coreTextLabel.documentModel=textArr;
    [_coreTextLabel setNeedsDisplay];
    [self setNeedsDisplay];
    
}

- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    
    if(_isLastParagraph){
        CTFrameRef ctFrameRef = [_coreTextLabel ctFrameRef];
        NSArray *lines = (NSArray *)CTFrameGetLines(ctFrameRef);
        CTLineRef line = (__bridge CTLineRef)[lines lastObject];
        
        NSArray *ctRuns = (NSArray *)CTLineGetGlyphRuns(line);
        CTRunRef ctRun = (__bridge CTRunRef)[ctRuns lastObject];
        
        CGFloat lineWidth = 0;
        CGFloat linef=0;
        if (ctRun) {
            lineWidth = (*CTRunGetPositionsPtr(ctRun)).x;
            linef =(*CTRunGetPositionsPtr(ctRun)).y;
            
        }
        CTLineGetTypographicBounds(line, NULL, NULL, NULL);
        // 只有一行时，需要考虑缩进
//        BOOL onlyOneLine = lines.count==1?YES:NO;
//        if (onlyOneLine) {
//            lineWidth += 2.0f*(TEXT_FONT_NORMARL_SIZE+ICurrentTheme.docStyle.textFontSizeLevel);
//        }
//        NSLog(@"%f",linef);
//        NSLog(@"---%f---%f",_coreTextLabel.height,(_coreTextLabel.height-linef));
        CGRect rc = CGRectMake(0, 0, 15, 15);
        if (lineWidth > GlobleWidth-20-15) {
            rc = CGRectMake(14, _coreTextLabel.height+12, 15, 15);
        }else{
            rc = CGRectMake(14+lineWidth, _coreTextLabel.height-8, 15, 15);
        }
        
        if(rc.origin.y<10)
            rc = CGRectMake(rc.origin.x, 3, rc.size.width, rc.size.height);
        if(!self.finishChanLinLogo){
            self.finishChanLinLogo=[UIImageView new];
            self.finishChanLinLogo.image=[UIImage imageNamed:@"doc_finish_logoIcon.png"];
            
        }
        self.finishChanLinLogo.frame=rc;
        [self addSubview:self.finishChanLinLogo];
//        UIImage *finishChanLinLogo = [UIImage imageNamed:@"doc_finish_logoIcon.png"];
//        [finishChanLinLogo drawInRect:rc];
        
    }else{
        [self.finishChanLinLogo removeFromSuperview];
        self.finishChanLinLogo=nil;
    }
}
@end

@interface IDocTextCell (){
    IDocTextCellView *_contenteView;
}

@end

@implementation IDocTextCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _contenteView = [[IDocTextCellView alloc] initWithFrame:self.contentView.bounds];
        //_contenteView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self.contentView addSubview:_contenteView];
    }
    return self;
}
- (void)setIsLastParagraph:(BOOL)isLastParagraph{
    _contenteView.isLastParagraph = isLastParagraph;
}
- (BOOL)isLastParagraph{
    return _contenteView.isLastParagraph;
}
- (void)fillDataWithTextArr:(CLDocumentModel *)textArr andElement:(id)element{
    [_contenteView fillDataWithTextArr:textArr andElement:element];
}
@end
